import { Connector } from "cax-connector";
import { API_EVENT, IReq, IRes } from "../interface";
import { v4 as uuidv4 } from "uuid";

export function freebody_read_file(this: Connector, data: any) {
  if (this.client == null) return Promise.reject("socket未初始化");
  const req: IReq = {
    Topic: "api",
    Type: API_EVENT.FREEBODY_READ_FILE,
    Params: data,
    Req: 1,
    Res: 0,
    Tag: uuidv4(),
  };
  this.client.emit("api", req);
  return new Promise((resolve, reject) => {
    this.client?.once(req.Tag, (ev: IRes) => {
      // console.log('result freebody_read_file: ', ev.Result)
      resolve(ev.Result);
    });
  });
}

export function freebody_enter(this: Connector, data: any) {
  if (this.client == null) return Promise.reject("socket未初始化");
  const req: IReq = {
    Topic: "api",
    Type: API_EVENT.FREEBODY_ENTER,
    Params: data,
    Req: 1,
    Res: 0,
    Tag: uuidv4(),
  };
  this.client.emit("api", req);
  return new Promise((resolve, reject) => {
    this.client?.once(req.Tag, (ev: IRes) => {
      // console.log('result freebody_enter: ', ev.Result)
      resolve(ev.Result);
    });
  });
}

export function freebody_leave(this: Connector, data: any) {
  if (this.client == null) return Promise.reject("socket未初始化");
  const req: IReq = {
    Topic: "api",
    Type: API_EVENT.FREEBODY_LEAVE,
    Params: data,
    Req: 1,
    Res: 0,
    Tag: uuidv4(),
  };
  this.client.emit("api", req);
  return new Promise((resolve, reject) => {
    this.client?.once(req.Tag, (ev: IRes) => {
      // console.log('result freebody_leave: ', ev.Result)
      resolve(ev.Result);
    });
  });
}

export function freebody_sets(this: Connector, data: any) {
  if (this.client == null) return Promise.reject("socket未初始化");
  const req: IReq = {
    Topic: "api",
    Type: API_EVENT.FREEBODY_SETS,
    Params: data,
    Req: 1,
    Res: 0,
    Tag: uuidv4(),
  };
  this.client.emit("api", req);
  return new Promise((resolve, reject) => {
    this.client?.once(req.Tag, (ev: IRes) => {
      // console.log('result freebody_sets: ', ev.Result)
      resolve(ev.Result);
    });
  });
}

export function freebody_update_entities(this: Connector, data: any) {
  if (this.client == null) return Promise.reject("socket未初始化");
  const req: IReq = {
    Topic: "api",
    Type: API_EVENT.FREEBODY_UPDATE_ENTITIES,
    Params: data,
    Req: 1,
    Res: 0,
    Tag: uuidv4(),
  };
  this.client.emit("api", req);
  return new Promise((resolve, reject) => {
    this.client?.once(req.Tag, (ev: IRes) => {
      // console.log('result freebody_update_entities: ', ev.Result)
      resolve(ev.Result);
    });
  });
}

export function freebody_find_nodes(this: Connector, data: any) {
  if (this.client == null) return Promise.reject("socket未初始化");
  const req: IReq = {
    Topic: "api",
    Type: API_EVENT.FREEBODY_FIND_NODES,
    Params: data,
    Req: 1,
    Res: 0,
    Tag: uuidv4(),
  };
  this.client.emit("api", req);
  return new Promise((resolve, reject) => {
    this.client?.once(req.Tag, (ev: IRes) => {
      // console.log('result freebody_find_nodes: ', ev.Result)
      resolve(ev.Result);
    });
  });
}

export function freebody_apply(this: Connector, data: any) {
  if (this.client == null) return Promise.reject("socket未初始化");
  const req: IReq = {
    Topic: "api",
    Type: API_EVENT.FREEBODY_APPLY,
    Params: data,
    Req: 1,
    Res: 0,
    Tag: uuidv4(),
  };
  this.client.emit("api", req);
  return new Promise((resolve, reject) => {
    this.client?.once(req.Tag, (ev: IRes) => {
      // console.log('result freebody_apply: ', ev.Result)
      resolve(ev.Result);
    });
  });
}
export function freebody_table_data(this: Connector, data: any) {
  if (this.client == null) return Promise.reject("socket未初始化");
  const req: IReq = {
    Topic: "api",
    Type: API_EVENT.FREEBODY_TABLE_DATA,
    Params: data,
    Req: 1,
    Res: 0,
    Tag: uuidv4(),
  };
  this.client.emit("api", req);
  return new Promise((resolve, reject) => {
    this.client?.once(req.Tag, (ev: IRes) => {
      // console.log('result table data: ', ev.Result)
      resolve(ev.Result);
    });
  });
}

